package com.yuxl.admin.manager.auth.core.repository;

import com.yuxl.admin.manager.auth.core.entity.AuthUser;
import org.springframework.data.r2dbc.repository.Query;
import org.springframework.data.r2dbc.repository.R2dbcRepository;
import org.springframework.data.repository.query.Param;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.time.LocalDateTime;

// 定义 AuthUser 的响应式数据库操作接口，继承自 R2dbcRepository
public interface AuthUserRepository extends R2dbcRepository<AuthUser, Long> {
    
    // 定义根据用户名查找用户的方法
    // 这个方法会被 Spring Data 自动实现
    // 返回一个 Mono<AuthUser>，表示一个异步的、可能为空的 AuthUser 结果
    @Query("""
        SELECT * FROM auth_user WHERE username = :username LIMIT 1
        """)
    Mono<AuthUser> findByUsername(@Param("username") String username);

    @Query("""
            SELECT * FROM auth_user WHERE create_time BETWEEN :startTime AND :endTime
            """)
    Flux<AuthUser> findByCreateTimeBetween(@Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime);

    @Query("SELECT * FROM auth_user WHERE username = :username AND create_time BETWEEN :startTime AND :endTime")
    Flux<AuthUser> findByUsernameAndCreateTimeBetween(@Param("username") String username, 
                                                      @Param("startTime") LocalDateTime startTime, 
                                                      @Param("endTime") LocalDateTime endTime);
}
